﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mds.Common
{
    public class PlatformServiceFactory
    {
        private static PlatformServiceFactory _instance = new PlatformServiceFactory();
        public static PlatformServiceFactory Instance
        {
            get
            {
                return _instance;
            }
        }      
        public T GetService<T>()
        {
            Type interfaceType = typeof(T);
            var interfaceName = interfaceType.ToString();
            if (!PlatformServiceCache.Instance.IsExists(interfaceName))
            {
                var classFullName = ClassConfigurationCache.Instance.GetServiceClass(interfaceName);
                if (!string.IsNullOrEmpty(classFullName))
                {
                    Type classObject = Type.GetType(classFullName, true);
                    var serviceClass = Activator.CreateInstance(classObject);
                    PlatformServiceCache.Instance.AddCache(interfaceName, serviceClass);
                }               
            }
            return (T)PlatformServiceCache.Instance.GetCache(interfaceName);
        }       
    }
}
